//
//  MYGHealthManagementShareView.swift
//  MYGrow
//
//  Created by 林矗 on 2024/4/14.
//

import UIKit
enum MYGHealthManagementShareType {
    case wechat
    case wechatCircle
    case qq
    case doctor
    
    var title: String {
        switch self {
        case .wechat:
            "微信"
        case .wechatCircle:
            "朋友圈"
        case .qq:
            "QQ"
        case .doctor:
            "沟通过的医生"
        }
    }
    
    var icon: String {
        switch self {
        case .wechat:
            "Health/Management/wechat"
        case .wechatCircle:
            "Health/Management/wechatcircle"
        case .qq:
            "Health/Management/qq"
        case .doctor:
            "Health/Management/doctor"
        }
    }
}

class MYGHealthManagementShareView: UIView {
    // MARK: - Property
    var action: ((Int)->())?
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        backgroundColor = .white
        addSubviews([
            titleLabel,
            stackView
                    ])
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(-20)
            make.left.equalTo(30)
        }
        
        stackView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(15)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(75)
            make.bottom.equalTo(-20-bottomSafeheight)
        }
        
        let shares: [MYGHealthManagementShareType] = [.wechat, .wechatCircle, .qq, .doctor]
        for (i, share) in shares.enumerated() {
            let view = createShareObjctView(title: share.title, icon: share.icon, index: i)
            stackView.addArrangedSubview(view)
        }
    }
    
    func createShareObjctView(title: String, icon: String, index: Int) -> UIView {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.distribution = .fill
        stackView.alignment = .center
        stackView.spacing = 4
        
        let iconImageView = UIImageView(image: UIImage(named: icon))
        let titleLabel = UILabel()
        titleLabel.font = .regular(14)
        titleLabel.textColor = .rgba(35, 35, 35)
        titleLabel.text = title
        stackView.addArrangedSubviews([
            iconImageView,
            titleLabel
        ])
        let button = UIButton()
        stackView.addSubview(button)
        button.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        button.tag = index
        button.addTarget(self, action: #selector(buttonEvent), for: .touchUpInside)
        return stackView
    }
    
    @objc func buttonEvent(sender: UIButton) {
        action?(sender.tag)
    }
    // MARK: - Public funcs
    
    // MARK: - lazy load
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .horizontal
        stackV.distribution = .fillEqually
        stackV.alignment = .center
        stackV.spacing = 10
        return stackV
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(16)
        label.textColor = .rgba(35, 35, 35)
        label.text = "分享至"
        return label
    }()
}
